package com.lehuowujie.lehuomarket.shop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;

import com.lehuowujie.lehuomarket.R;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 王瑞铭 on 2017/1/16.
 */
public class ShopAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private LayoutInflater mInflater;

    private List<ParentEntity> mParentList;

    /**
     * 记录父控件的选中情况
     */
    private static Set<String> parentSelectSet = new HashSet<>();

    /**
     * 记录子控件的选中情况
     */
    private static Set<String> childSelectSet = new HashSet<>();

    public ShopAdapter(Context context, List<ParentEntity> list) {
        this.mContext = context;
        this.mParentList = list;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getGroupCount() {
        return mParentList.size();
    }

    @Override
    public int getChildrenCount(int position) {
        return mParentList.get(position).getShopList().size();
    }

    @Override
    public Object getGroup(int position) {
        return mParentList.get(position);
    }

    @Override
    public Object getChild(int position, int childPosition) {
        return mParentList.get(position).getShopList().get(childPosition);
    }

    @Override
    public long getGroupId(int position) {
        return position;
    }

    @Override
    public long getChildId(int position, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int position, boolean b, View view, ViewGroup viewGroup) {
        if (view == null)
            view = getGroupView(viewGroup);
        setGroupData(view);
        initGroupEvent(view, position);
        return view;
    }

    @Override
    public View getChildView(int position, int childPosition, boolean b, View view, ViewGroup viewGroup) {
        if (view == null)
            view = getChildView(viewGroup);
        initChildEvent(view, position, childPosition);
        return view;
    }

    @Override
    public boolean isChildSelectable(int position, int childPosition) {
        return false;
    }

    /**
     * 获取父控件的布局
     *
     * @param parent
     * @return
     */
    private View getGroupView(ViewGroup parent) {
        return mInflater.inflate(R.layout.item_parent_layout, parent, false);
    }

    /**
     * 设置父控件显示数据
     *
     * @param view
     */
    private void setGroupData(View view) {
    }

    /**
     * 绑定父控件的事件
     *
     * @param view
     */
    private void initGroupEvent(View view, int position) {
        final ImageView parent_check = (ImageView) view.findViewById(R.id.parent_check);
        final ParentEntity entity = mParentList.get(position);
        final List<ChildEntity> childList = entity.getShopList();
        if (parentSelectSet.contains(entity.getParentId())) {
            //选中
            parent_check.setImageResource(R.mipmap.checked);
        } else {
            parent_check.setImageResource(R.mipmap.unchecked);
        }
        parent_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (parentSelectSet.contains(entity.getParentId())) {
                    //置为未选中并移除记录的对应的所有子项
                    parentSelectSet.remove(entity.getParentId());
                    if (childList != null && childList.size() > 0) {
                        for (ChildEntity childEntity : childList) {
                            if (childSelectSet.contains(childEntity.getId())) {
                                childSelectSet.remove(childEntity.getId());
                            }
                        }
                    }
                    //通知UI取消全选
                    if (mListener!=null)
                        mListener.isCheckedAll(false);
                } else {
                    //置为选中并记录所有对应的子项
                    parentSelectSet.add(entity.getParentId());
                    if (childList != null && childList.size() > 0) {
                        for (ChildEntity childEntity : childList) {
                            if (!childSelectSet.contains(childEntity.getId())) {
                                childSelectSet.add(childEntity.getId());
                            }
                        }
                    }
                    if (isParentCheckAll())
                    {
                        if (mListener!=null)
                            mListener.isCheckedAll(true);
                    }else {
                        if (mListener!=null)
                            mListener.isCheckedAll(false);
                    }
                }
                notifyDataSetChanged();
            }
        });
    }

    /**
     * 获取子控件的布局
     *
     * @param parent
     * @return
     */
    private View getChildView(ViewGroup parent) {
        return mInflater.inflate(R.layout.item_child_layout, parent, false);
    }

    /**
     * 绑定子控件的事件
     *
     * @param view
     * @param position
     * @param ChildPosition
     */
    private void initChildEvent(View view, final int position, int ChildPosition) {
        ImageView child_check = (ImageView) view.findViewById(R.id.child_check);
        final ParentEntity parentEntity = mParentList.get(position);
        final List<ChildEntity> childList = parentEntity.getShopList();
        final ChildEntity entity = mParentList.get(position).getShopList().get(ChildPosition);

        if (childSelectSet.contains(entity.getId())) {
            child_check.setImageResource(R.mipmap.checked);
        } else {
            child_check.setImageResource(R.mipmap.unchecked);
        }

        child_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (childSelectSet.contains(entity.getId())) {
                    //置为未选中
                    childSelectSet.remove(entity.getId());
                    //判断其父控件是否记录为选中状态
                    if (parentSelectSet.contains(parentEntity.getParentId())) {
                        //如果是：移除记录
                        parentSelectSet.remove(parentEntity.getParentId());
                    }
                    //通知UI取消全选
                    if (mListener!=null)
                        mListener.isCheckedAll(false);
                } else {
                    //置为选中
                    childSelectSet.add(entity.getId());
                    //记录父控件下的子控件是否全部选中
                    boolean isChildCheckAll = true;
                    //判断是否对应父控件中的所有子控件都处于选中状态
                    for (ChildEntity child : childList)
                    {
                        if (!childSelectSet.contains(child.getId()))
                        {
                            //存在子控件未选中
                            isChildCheckAll = false;
                        }
                    }
                    if (isChildCheckAll)
                    {
                        if (!parentSelectSet.contains(parentEntity.getParentId()))
                        {
                            parentSelectSet.add(parentEntity.getParentId());
                        }
                    }
                    if (isParentCheckAll())
                    {
                        if (mListener!=null)
                            mListener.isCheckedAll(true);
                    }else {
                        if (mListener!=null)
                            mListener.isCheckedAll(false);
                    }
                    //如果是，将对应的父控件置为未选中
                    //然后判断是否
                }
                notifyDataSetChanged();
            }
        });

    }

    /**
     * 判断父控件是否全部选中
     *
     * @return
     */
    private boolean isParentCheckAll() {
        if (parentSelectSet.size() == mParentList.size())
            return true;
        return false;
    }

    //-----------------------------------------------------------------

    public onCheckAllListener mListener;

    public interface onCheckAllListener {
        void isCheckedAll(boolean isCheckedAll);
    }

    public void setOnCheckAllListener(onCheckAllListener listener) {
        this.mListener = listener;
    }

    /**
     * 获取所有选中的父控件的数据
     *
     * @return
     */
    public List<ParentEntity> getSelectParentItem() {
        return null;
    }

    /**
     * 获取所有选中的子控件的数据
     *
     * @return
     */
    public List<ChildEntity> getSelectChildItem() {
        return null;
    }

}
